package com.jyy.prefabricated.service.impl;

import com.jyy.prefabricated.entity.OrderInfo;
import com.jyy.prefabricated.entity.QuizRecord;
import com.jyy.prefabricated.mapper.OrderInfoMapper;
import com.jyy.prefabricated.mapper.QuizRecordMapper;
import com.jyy.prefabricated.service.QuizRecordService;
import com.jyy.prefabricated.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 学生随堂考记录表(QuizRecord)表服务实现类
 *
 * @author makejava
 * @since 2021-10-11 14:05:47
 */
@Service("quizRecordService")
public class QuizRecordServiceImpl implements QuizRecordService {
    @Resource
    private QuizRecordMapper quizRecordMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QuizRecord queryById(Long id) {
        return this.quizRecordMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<QuizRecord> queryAllByLimit(int offset, int limit) {
        return this.quizRecordMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param quizRecord 实例对象
     * @return 实例对象
     */
    @Override
    public QuizRecord insert(QuizRecord quizRecord) {
        this.quizRecordMapper.insert(quizRecord);
        return quizRecord;
    }

    /**
     * 修改数据
     *
     * @param quizRecord 实例对象
     * @return 实例对象
     */
    @Override
    public QuizRecord update(QuizRecord quizRecord) {
        this.quizRecordMapper.update(quizRecord);
        return this.queryById(quizRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.quizRecordMapper.deleteById(id) > 0;
    }

    @Override
    public List<QuizRecord> selectByParams(QuizRecord quizRecord) {
        List<QuizRecord> result = new ArrayList<>();
        List<QuizRecord> quizRecordList = quizRecordMapper.queryAll(quizRecord);
        if (quizRecordList != null && quizRecordList.size() > 0) {
            for (QuizRecord item : quizRecordList) {
                if (whetherOverdue(item.getOrderId())) {
                    result.add(item);
                }
            }
        }
        return result;
    }

    @Override
    public List<QuizRecord> selectByStudentIdAndOrderId(QuizRecord quizRecord) {
        List<QuizRecord> result = quizRecordMapper.queryAll(quizRecord);
        return result;
    }


    //判断商品（课程包）是否过期
    public Boolean whetherOverdue(Integer orderId) {
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
        if (orderInfo != null && orderInfo.getPayTime() != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date currdate = format.parse(orderInfo.getPayTime());
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                if (orderInfo.getBuyType() != null && orderInfo.getBuyTypeNum() != null) {
                    if (orderInfo.getBuyType() == 0) {
                        ca.add(Calendar.YEAR, orderInfo.getBuyTypeNum());
                    } else if (orderInfo.getBuyType() == 1) {
                        ca.add(Calendar.MONTH, orderInfo.getBuyTypeNum());
                    } else if (orderInfo.getBuyType() == 2) {
                        ca.add(Calendar.WEEK_OF_YEAR, orderInfo.getBuyTypeNum());
                    } else if (orderInfo.getBuyType() == 3) {
                        ca.add(Calendar.DATE, orderInfo.getBuyTypeNum());
                    }
                }
                currdate = ca.getTime();
                long now = DateUtils.getUct8NowDate().getTime();
                if (currdate.getTime() < now) {
                    return false;
                }
            } catch (ParseException e) {
            }

        }
        return true;
    }
}
